var AddThis_MainButton = {
	
	setVisibility : function(visible, doc) {
		AddThis_Util.dbg("MainButton.setVisibility: enter");
        if (typeof doc == "undefined") doc = document;
		var toolbox = doc.getElementById("navigator-toolbox");
		var toolboxDocument = toolbox.ownerDocument;
		var currentlyVisible = this.isVisible(doc); 
		if (currentlyVisible && !visible) {

			
			AddThis_Util.dbg("MainButton.setVisibility: removing button from bar...");
			for (var i = 0; i < toolbox.childNodes.length; ++i) {
				var toolbar = toolbox.childNodes[i];
				if (toolbar.localName == "toolbar" && toolbar.getAttribute("customizable") == "true" 
					&& toolbar.id=="nav-bar") {
					AddThis_Util.dbg("MainButton.setVisibility: currentSet = " + toolbar.currentSet);
					
					
					var idx = toolbar.currentSet.indexOf("at_toolbarBtn");
					if (idx != -1) {
						var preUs = toolbar.currentSet.substring (0, idx);
						var postUs = toolbar.currentSet.substring (idx + 14, toolbar.currentSet.length);
						var newSet = preUs + postUs;
						AddThis_Util.dbg("MainButton.setVisibility: setting to new currentSet: " + newSet);
						toolbar.currentSet = newSet;
						toolbar.setAttribute("currentset", newSet);
						toolboxDocument.persist(toolbar.id, "currentset");
						AddThis_Util.dbg("MainButton.setVisibility: button removed.");
						continue;
					}
				}
			}
		} else if (!currentlyVisible && visible) {

			
			AddThis_Util.dbg("MainButton.setVisibility: adding button to bar...");
			for (var i = 0; i < toolbox.childNodes.length; ++i) {
				toolbar = toolbox.childNodes[i];
				if (toolbar.localName == "toolbar" && toolbar.getAttribute("customizable") == "true" 
					&& toolbar.id=="nav-bar") {
					
					
					var idx = toolbar.currentSet.indexOf("urlbar-container");
					if (idx != -1) {
						var preBar = toolbar.currentSet.substring (0, idx);
						var postBar = toolbar.currentSet.substring (idx, toolbar.currentSet.length);
						var newSet = preBar + "at_toolbarBtn," + postBar;
						toolbar.currentSet = newSet;
						toolbar.setAttribute("currentset", newSet);
						toolboxDocument.persist(toolbar.id, "currentset");
						AddThis_Util.dbg("MainButton.setVisibility: button added.");
						continue;
					}
				}
			}
		}
		AddThis_Util.dbg("MainButton.setVisibility: finished toolbox customization");
        try{
            doc.defaultView.BrowserToolboxCustomizeDone(true);
        } catch(e){}
		AddThis_Util.dbg("MainButton.setVisibility: done.");
	},

	isVisible : function(doc) {
		AddThis_Util.dbg("MainButton.isVisible: looking for our button...");
        if (typeof doc == "undefined") doc = document;
		var toolbox = doc.getElementById("navigator-toolbox");
		var toolboxDocument = toolbox.ownerDocument;
		var hasButton = false;
		for (var i = 0; i < toolbox.childNodes.length; ++i) {
			var toolbar = toolbox.childNodes[i];
			if (toolbar.localName == "toolbar" && toolbar.getAttribute("customizable") == "true") {
				if(toolbar.currentSet.indexOf("at_toolbarBtn") != -1) {
					hasButton = true;
					continue;
				}
			}
		}
		AddThis_Util.dbg("MainButton.isVisible: found button? " + hasButton);
		return hasButton;
	},
    
    prepare: function() {
        AddThis_Util.dbg("MainButton.prepare: go...");
        AddThis_Sharing.initShareMenu("at_toolbarBtnPopup");
    }

}